"use strict";

(function () {
  function r(e, n, t) {
    function o(i, f) {
      if (!n[i]) {
        if (!e[i]) {
          var c = "function" == typeof require && require;
          if (!f && c) return c(i, !0);
          if (u) return u(i, !0);
          var a = new Error("Cannot find module '" + i + "'");
          throw a.code = "MODULE_NOT_FOUND", a;
        }

        var p = n[i] = {
          exports: {}
        };
        e[i][0].call(p.exports, function (r) {
          var n = e[i][1][r];
          return o(n || r);
        }, p, p.exports, r, e, n, t);
      }

      return n[i].exports;
    }

    for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) {
      o(t[i]);
    }

    return o;
  }

  return r;
})()({
  1: [function (require, module, exports) {
    "use strict"; // const { data } = require("autoprefixer");

    loader.define({
      depend: ['pages/config/config'],
      // beforeLoad: function(config){
      //     var script = document.createElement("script");  
      //     script.src = "http://api.map.baidu.com/api?v=2.0&ak=LniWrEprri0pDMBzhFpInn19B6TDPed5";
      //     document.body.appendChild(script); 
      // },
      loaded: function loaded(config, require, exports, module, global) {
        this.pageview = {}; // function refresh() {
        //     // uiPullRefresh.setHeight(300);
        //     bui.refresh();
        // }
        // function setheight() { 
        //     uiPullRefresh.setHeight(300);
        // }
        // var uiPullRefresh = bui.pullrefresh({
        //     id: "#panel-search",
        //     onRefresh: refresh,
        //     // onBeforeRefresh: setheight,
        //     autoLoad: false,
        //     stopHandle: ".bui-scroll-x",
        // })

        function getData() {
          var map = new BMap.Map();
          var geolocation = new BMap.Geolocation();
          geolocation.getCurrentPosition(function (r) {
            var city;

            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
              // 取到城市
              console.log(r.address);
              console.log(r.point);
              city = r.address.city; // 取到经纬度

              var currentp = r.point;
              console.log(city);
              city = '威海市';
              var data_ = {
                // 这里是获取的city
                city: city
              };
              data_ = JSON.stringify(data_);
              bui.ajax({
                // url: 相对路径
                url: config.apiurl + "api/v1.0/searchact",
                data: data_,
                method: "post",
                headers: {
                  'Content-Type': 'application/json'
                }
              }).then(function (res) {
                console.log(res); // var msg_type = ['游戏/娱乐', '学习/交流', '户外/休闲', '运动/健身','自定义']

                var msg = {
                  "游戏/娱乐": {
                    'subname': 'entertainment',
                    'data': []
                  },
                  "学习/交流": {
                    'subname': 'study',
                    'data': []
                  },
                  "户外/休闲": {
                    'subname': 'outdoor',
                    'data': []
                  },
                  "运动/健身": {
                    'subname': 'sports',
                    'data': []
                  },
                  "自定义": {
                    'subname': 'userdefined',
                    'data': []
                  }
                };
                var activity = res['activities'];

                for (var i in activity) {
                  var type = activity[i]["type"];
                  var lng = activity[i]['longitude'];
                  var lat = activity[i]['latitude'];
                  var destp = new BMap.Point(lng, lat);
                  var dis = map.getDistance(destp, currentp);
                  var detail = {
                    "id": activity[i]["id"],
                    "name": activity[i]["name"],
                    "pageview": activity[i]["pageview"],
                    "distance": dis.toFixed(2),
                    "cmp": activity[i]["hot"] - dis,
                    "lng": lng,
                    "lat": lat,
                    "type": type,
                    "peoplesurplus": activity[i]["peoplesurplus"],
                    "is_involved": activity[i]["is_involve"]
                  };
                  var s = detail['name'];
                  detail['name'] = s.length > 5 ? s.substring(0, 5) + '...' : s;

                  if (dis >= 1000) {
                    detail['distance'] = (dis / 1000).toFixed(2);
                    detail['unit'] = 'km';
                  } else {
                    detail['distance'] = dis.toFixed(0);
                    detail['unit'] = 'm';
                  }

                  msg[type]['data'].push(detail);
                }

                loadhtml(msg);
              }, function (res, status) {
                console.log(status);
              });
            } else {
              // alert('failed'+this.getStatus());
              bui.hint({
                content: "<i class='icon-error'></i><br />请检查是否开启GPS",
                position: "center",
                effect: "fadeInDown"
              });
            }
          }, {
            enableHighAccuracy: true
          });
        }

        function loadhtml(msg) {
          function rule(a, b) {
            return b['cmp'] - a['cmp'];
          }

          for (var i in msg) {
            var tmsg = msg[i]['data']; // console.log(tmsg);

            tmsg.sort(rule);
            console.log(tmsg);
            var count = 0;

            for (var j in tmsg) {
              count = count + 1;
              var searchItem = $('<li class="search-item" style="width: 2.5rem;"></li>');
              var btn = $('<div class="bui-btn item-btn"></div>');
              btn.attr("id", tmsg[j]['id']);
              var btnPic = $('<div class="bui-pic"></div>');
              var itemName = $('<div class="bui-box item-name"></div>');
              itemName.text(tmsg[j]['name']);
              btnPic.append(itemName); // itemDistance

              var itemDistance = $('<div class="bui-box-align-middle item-detail item-dis"></div>');
              itemDistance.append($('<img src="images/search/place_16.png">'));
              itemDistance.append($('<div>' + tmsg[j]['distance'] + tmsg[j]['unit'] + '</div>'));
              btnPic.append(itemDistance); // itemView

              var itemView = $('<div class="bui-box-align-middle item-detail"></div>');
              itemView.append($('<img src="images/search/look_16.png">'));
              itemView.append($('<div>' + tmsg[j]['pageview'] + '</div>'));
              btnPic.append(itemView); // itemLng

              var itemLng = $('<p></p>');
              itemLng.attr('id', 'lng' + tmsg[j]['id']);
              itemLng.text(tmsg[j]['lng']);
              itemLng.hide();
              btnPic.append(itemLng); // itemLat

              var itemLat = $('<p></p>');
              itemLat.attr('id', 'lat' + tmsg[j]['id']);
              itemLat.text(tmsg[j]['lat']);
              itemLat.hide();
              btnPic.append(itemLat); // itemType

              var itemType = $('<p></p>');
              itemType.attr('id', 'type' + tmsg[j]['id']);
              itemType.text(tmsg[j]['type']);
              itemType.hide();
              btnPic.append(itemType);
              var full = $('<i class="icon-tag"><img src="images/search/full.png"></i>');
              if (tmsg[j]['peoplesurplus'] <= 0) btnPic.append(full);
              var involve = $('<i class="icon-tag"><img src="images/search/involve.png"></i>');
              if (tmsg[j]['is_involved']) btnPic.append(involve);
              btn.append(btnPic);
              searchItem.append(btn);
              $('#' + msg[i]['subname']).append(searchItem);
              if (count == 10) break;
            }

            var more = $('<li class="search-item" style="width: 2.5rem;"><div class="bui-btn more"><div class="bui-pic bui-box-center"><span class="more-content">点击查看更多</span><span class="tag" style="display:none">' + i + '</span></div></div></li>');
            $('#' + msg[i]['subname']).append(more);
          }

          $(".more").on("click", function (e) {
            var tag = $(this).find('.tag');
            console.log(tag);
            tag = tag[0];
            console.log($(tag).text());
            bui.load({
              url: 'pages/searchmore/searchmore',
              param: {
                msg: msg[$(tag).text()]['data'],
                type: $(tag).text()
              }
            });
          });
          $(".item-btn").on("click", function (e) {
            // 将活动id作为唯一标识，传给detail界面
            // alert("hhh");
            // alert($(this).attr('id'));
            var id = $(this).attr('id');
            var lng = $('#lng' + id).text();
            var lat = $('#lat' + id).text();
            var type = $('#type' + id).text(); // console.log(lat);
            // console.log(type);

            bui.load({
              url: "pages/activitydetail/activitydetail",
              param: {
                id: id,
                type: type,
                lng: lng,
                lat: lat
              }
            });
          }); // if($('.irefresh img').hasClass('refreshing'))
          //     $('.irefresh img').removeClass('refreshing')

          $('.panel-loading').hide();
        }

        this.pageview.init = function () {
          // $('.irefresh img').addClass('refreshing');
          $('.panel-loading').show();
          getData(); // setTimeout("$('.irefresh img').removeClass('refreshing')", 1000);
        };

        this.pageview.init();

        this.pageview.refresh = function () {
          location.reload();
        };

        return this.pageview;
      },
      show: function show(e) {// if(e.type == 'load' || e.type == 'back')
        //     this.pageview.refresh();
      }
    });
  }, {}]
}, {}, [1]);